#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;

void PrintContacts(contacts::Contacts &contacts1){
    for(int i = 0; i < contacts1.contacts_size(); i++){
        cout << "---------------这是联系人" << i + 1 << "的信息---------------" << endl;
        const contacts::PeopleInfo people = contacts1.contacts(i);
        cout << "姓名：" << people.name() << endl;
        cout << "年龄：" << people.age() << endl;
        cout << "职业：" << people.profession() << endl;
        cout << "薪水：" << people.salary() << endl;

        for(int j = 0; j < people.phone_size(); j++){
            const contacts::PeopleInfo_Phone &phones = people.phone(j);
            cout << "电话号码[" << phones.PhoneType_Name(phones.type()) << "]";
            cout << j + 1 << ": " << phones.number() << endl;
        }

        if(people.has_data() && people.data().Is<contacts::Address>()){
            contacts::Address address;
            people.data().UnpackTo(&address);
            cout << "家庭住址为：" << address.home_address() << endl;
            cout << "单位住址为：" << address.unit_address() << endl;
        }

        switch (people.other_contacts_case())
        {
        case contacts::PeopleInfo::OtherContactsCase::kQq:
            cout << "QQ：" << people.qq() << endl;
            break;
        case contacts::PeopleInfo::OtherContactsCase::kWechat:
            cout << "微信：" << people.wechat() << endl;
            break;
        case contacts::PeopleInfo::OtherContactsCase::kEmail:
            cout << "Email：" << people.email() << endl;
            break;
        default:
            break;
        }

        if(people.remark_size()){
            for(auto it = people.remark().cbegin(); it != people.remark().cend(); ++it){
                cout << "备注标题[" << it->first<< "]" << it->second << endl;
            }
        }

    }
}

int main(){
    contacts::Contacts contacts1;
    fstream input("contacts.bin", ios::in | ios:: binary);
    if(!contacts1.ParseFromIstream(&input)){
        cerr << "反序列化失败！！！" << endl;
        input.close();
        return -1;
    }

    // 打印联系人信息
    PrintContacts(contacts1);

    return 0;
}